package handles

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vehicle-mgr/cache"
	"vehicle-mgr/models"
)

func PerAdd(c *gin.Context) {

	per_code := c.PostForm("per_code")
	per_name := c.PostForm("per_name")
	per_address := c.PostForm("per_address")

	if per_code == "" || per_name == "" || per_address == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.Per{

		PerCode:    per_code,
		PerName:    per_name,
		PerAddress: per_address,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func PerOrder(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	pid, _ := strconv.Atoi(c.PostForm("pid"))

	if order_code == "" || order_name == "" || pid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "数据不能为空",
		})
		return
	}

	data := models.PerOrder{

		OrderCode: order_code,
		OrderName: order_name,
		Pid:       pid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func PerOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	/*
		SELECT
		  per_orders.id AS OrderId,
		  pers.per_code AS PerCode,
		  pers.per_name AS PerName,
		  pers.per_address AS PerAddress,
		  per_orders.order_code AS OrderCode,
		  per_orders.order_name AS OrderName
		FROM
		  per_orders
		  LEFT JOIN pers ON pers.id = per_orders.pid
	*/
	var per []models.PerOrderList
	redi_key := "list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redi_key).Result()
	if cacheData == "" {
		models.DB.Table("per_orders").
			Select(
				"per_orders.id AS OrderId",
				"pers.per_code AS PerCode",
				"pers.per_name AS PerName",
				"pers.per_address AS PerAddress",
				"per_orders.order_code AS OrderCode",
				"per_orders.order_name AS OrderName",
			).
			Joins("LEFT JOIN pers ON pers.id = per_orders.pid").
			Offset(offset).Limit(size).Find(&per)
		jsonData, _ := json.Marshal(per)
		cache.Rdb.Set(cache.Ctx, redi_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &per)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": per,
	})
	return
}

func PerOrderDetail(c *gin.Context) {
	id := c.Query("id")

	/*
		SELECT
		  per_orders.id AS OrderId,
		  pers.per_code AS PerCode,
		  pers.per_name AS PerName,
		  pers.per_address AS PerAddress,
		  per_orders.order_code AS OrderCode,
		  per_orders.order_name AS OrderName
		FROM
		  per_orders
		  LEFT JOIN pers ON pers.id = per_orders.pid
	*/
	var per models.PerOrderList
	redi_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redi_key).Result()
	if cacheData == "" {
		models.DB.Table("per_orders").
			Select(
				"per_orders.id AS OrderId",
				"pers.per_code AS PerCode",
				"pers.per_name AS PerName",
				"pers.per_address AS PerAddress",
				"per_orders.order_code AS OrderCode",
				"per_orders.order_name AS OrderName",
			).
			Joins("LEFT JOIN pers ON pers.id = per_orders.pid").
			Where("per_orders.id", id).Find(&per)
		jsonData, _ := json.Marshal(per)
		cache.Rdb.Set(cache.Ctx, redi_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &per)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": per,
	})
	return
}
